package com.meryoung.day2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.reporters.Files;

import static java.lang.Thread.sleep;

public class Alert {
    WebDriver driver;
    @BeforeMethod
    public void openchromeTest01(){
        //设置chromedriver路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\123\\IdeaProjects\\selenium1\\drivers\\chromedriver.exe");
        //实例化Chromedriver
        driver = new ChromeDriver();
    }
    /*
    打开ui自动化测试页面
    点击Alert按钮
    在alert警告框点击确定按钮
     */
    @Test
    public void alertTest() throws InterruptedException {
        driver.get("C:\\Users\\123\\Desktop\\appium\\index.html");
        sleep(3000);
        driver.findElement(By.className("alert")).click();
        Thread.sleep(2000);
        //把控制权转交给alert
        org.openqa.selenium.Alert alert = driver.switchTo().alert();
        Thread.sleep(2000);
        alert.accept();

    }
    /*
打开ui自动化测试页面
点击Confirm按钮
在Confirm警告框点击取消按钮
再次点击确定按钮
 */
    @Test
    public void confirmTest() throws InterruptedException {
        driver.get("C:\\Users\\123\\Desktop\\appium\\index.html");
        driver.findElement(By.className("confirm")).click();
        Thread.sleep(2000);
        //driver控制权转交给confirm
        org.openqa.selenium.Alert confirm = driver.switchTo().alert();
        //点击取消
        confirm.dismiss();
        Thread.sleep(2000);
        //点击确定
        confirm.accept();
    }
    /*
打开ui自动化测试页面
点击Prompt按钮
在Prompt警告框输入“这是个Prompt”
点击确定
再次点击确定按钮
 */
    @Test
    public void promptTest() throws InterruptedException {
        driver.get("C:\\Users\\123\\Desktop\\appium\\index.html");
        driver.findElement(By.className("prompt")).click();
        Thread.sleep(2000);
        org.openqa.selenium.Alert prompt = driver.switchTo().alert();
        Thread.sleep(2000);
        prompt.sendKeys("这是一个prompt");
        Thread.sleep(2000);
        prompt.accept();
        Thread.sleep(2000);
        prompt.accept();


    }
    @AfterMethod
    public void closedBrowser(){
        driver.quit();
    }
}
